<?php
// -----------------------------------------------------
// Created by : php-orm
// -----------------------------------------------------
// Date: 2021/5/27
// -----------------------------------------------------
// Time: 12:55
// -----------------------------------------------------
// User: tf <1096392101@qq.com>
// -----------------------------------------------------
declare (strict_types=1);

namespace Gao\orm\db;


class Query
{
    /**
     * pdo连接
     * @var ConnectionInterface
     */
    protected $pdo;

    /**
     * sql处理
     * @var Builder
     */
    protected $build;

    /**
     * 查询对象
     * @var array
     */
    protected $options = [
        'table'     => '',
        'alias'     => [],
        'where'     => [],
        'whereNum'  => 0,
        'field'     => '*',
        'order'     => [],
        'distinct'  => false,
        'join'      => [],
        'union'     => '',
        'group'     => '',
        'having'    => '',
        'limit'     => '',
        'extra'     => '',
        'lock'      => false,
        'fetch_sql' => false,
        'data'      => [],
        'prefix'    => 'tp_',
        'setDefer'  => true,
        'force'     => '',
        'comment'   => '',
    ];

    public function __construct($pdo,$build)
    {
        $this->pdo = new $pdo();
        $this->build = new $build();
        $this->setPrefix();
    }

    /**
     * 设置表名
     * @param $table
     * @return $this
     */
    public function table($table): Query
    {
        $this->options["table"] = $table;
        return $this;
    }

    /**
     * 查询
     * @return array
     * @throws \Exception
     */
    public function select(): array
    {
        $sql = $this->build->select($this);

        $this->pdo->getPDOStatement($sql);

        return $this->pdo->getResult();
    }

    /**
     * 插入
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function insert($data)
    {
        $this->options['data'] = $data;

        $sql = $this->build->insert($this);

        $this->pdo->getPDOStatement($sql);

        return $this->pdo->getRowCount();
    }

    /**
     * 更新
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function update($data)
    {
        $this->options['data'] = $data;

        $sql = $this->build->update($this);

        $this->pdo->getPDOStatement($sql);

        return $this->pdo->getRowCount();
    }

    /**
     * 删除
     * @return mixed
     * @throws \Exception
     */
    public function delete()
    {
        $sql = $this->build->delete($this);
        $this->pdo->getPDOStatement($sql);

        return $this->pdo->getRowCount();
    }

    /**
     * 设置查询的额外参数
     * @access public
     * @param string $extra 额外信息
     * @return $this
     */
    public function extra(string $extra): Query
    {
        $this->options['extra'] = $extra;
        return $this;
    }

    /**
     * 字段
     * @param string $field
     * @return $this
     */
    public function field(string $field): Query
    {
        if (!empty($field)) {
            $this->options['field'] = explode(',',$field);
        }
        return $this;
    }

    /**
     * where条件
     * @param array $where
     * @return $this
     */
    public function where(array $where): Query
    {
        $this->options['where'] = $where;
        return $this;
    }

    /**
     * @param int $offset
     * @param int|null $length
     * @return $this
     */
    public function limit(int $offset,int $length = null):Query
    {
        $this->options['limit'] = $offset . ($length ? ',' . $length : '');

        return $this;
    }

    /**
     * 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])
     * @access public
     * @param string|array $field 排序字段
     * @param string           $order 排序
     * @return $this
     */
    public function order($field, string $order = ''): Query
    {
        if (is_string($field)) {
            $this->options['order'] = [$field => $order];
        }else {
            if (is_array($field)) {
                $this->options['order'] = $field;
            }
        }

        return $this;
    }

    /**
     * 指定group查询
     * @access public
     * @param string|array $group GROUP
     * @return $this
     */
    public function group($group): Query
    {
        $this->options['group'] = $group;
        return $this;
    }

    /**
     * 指定having查询
     * @access public
     * @param string $having having
     * @return $this
     */
    public function having(string $having): Query
    {
        $this->options['having'] = $having;
        return $this;
    }

    /**
     * 查询注释
     * @access public
     * @param string $comment 注释
     * @return $this
     */
    public function comment(string $comment): Query
    {
        $this->options['comment'] = $comment;
        return $this;
    }

    /**
     * 锁机制
     * @param bool $lock
     * @return $this
     */
    public function lock(bool $lock): Query
    {
        $this->options['lock'] = $lock;
        return $this;
    }

    /**
     * 设置前缀
     */
    public function setPrefix()
    {
        $this->options['prefix'] = $this->pdo->getPrefix();
    }

    /**
     * @return array
     */
    public function getOptions(): array
    {
        return $this->options;
    }

    /**
     * 启动事务
     */
    public function startTrans(): void
    {
        $this->pdo->startTrans();
    }

    /**
     * 用于非自动提交状态下面的查询提交
     */
    public function commit(): void
    {
        $this->pdo->commit();
    }

    /**
     * 事务回滚
     */
    public function rollback(): void
    {
        $this->pdo->rollback();
    }

}